package com.mashibing.juc.c_026_01_ThreadPool;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * 假设你能够提供一个服务
 * 这个服务查询各大电商网站同一类产品的价格并汇总展示
 * CompletableFuture各种任务的管理类
 */
public class T06_01_CompletableFuture {

    public static void main(String[] args) {
        long start , end;

        /*start = System.currentTimeMillis();
        priceOfJD();
        priceOfTB();
        priceOfTM();
        end = System.currentTimeMillis();
        System.out.println("use completable future! "+(end - start));*/

        start=System.currentTimeMillis();
       /* CompletableFuture<Double> futureTM = CompletableFuture.supplyAsync(()->priceOfTM());
        CompletableFuture<Double> futureJD = CompletableFuture.supplyAsync(()->priceOfJD());
        CompletableFuture<Double> futureTB = CompletableFuture.supplyAsync(()->priceOfTB());

        CompletableFuture.allOf(futureTM,futureTB,futureJD).join();//allOf表示所有任务全部完成之后，anyOf表示只要有一个任务完成就行
        CompletableFuture.anyOf(futureTM,futureTB,futureJD).join();*/
        CompletableFuture.supplyAsync(()->priceOfTM())
                .thenApply(String::valueOf)
                .thenApply(str -> "price "+str)
                .thenAccept(System.out::println);
        end = System.currentTimeMillis();
        System.out.println("use completable future! "+(end - start));

        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static double priceOfTM(){
        dealy();
        return 1.00;
    }

    private static double priceOfTB(){
        dealy();
        return 2.00;
    }

    private static double priceOfJD(){
        dealy();
        return 3.00;
    }

    private static void dealy(){
        int time = new Random().nextInt(500);
        try {
            TimeUnit.MILLISECONDS.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.printf("After %s sleep!\n",time);
    }
}
